package com.zretc.service.impl;

import com.zretc.dao.*;
import com.zretc.entity.*;
import com.zretc.service.FrontFilmDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FrontFilmDetailsServiceImpl implements FrontFilmDetailsService {
    @Autowired
    private ScreeningMapper screeningMapper;
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private ActorMapper actorMapper;
    @Autowired
    private FilmTypeMapper filmTypeMapper;

    @Override
    public Map<String, Object> getFilmDetailPageData(Integer filmId) {
        HashMap<String, Object> map = new HashMap<>();
        // 电影名称
        Film film = filmMapper.findFilmByFilmId(filmId);

        map.put("filmName",film.getFilmName());
        // 分类
        List<FilmType> filmTypes = filmTypeMapper.getFilmTypes(filmId);
        map.put("filmTypes",filmTypes);
        // 时长
        map.put("filmRunTime",film.getFilmRunTime());
        // 上映时间
        map.put("filmReleaseTime",film.getFilmReleaseTime());
        // 封面地址
        map.put("filmCover",film.getFilmCover());
        // 电影介绍
        map.put("filmContent",film.getFilmContent());
        // 演员
        List<Actor> actors = actorMapper.getActorList(filmId);
        map.put("actors",actors);
        // 预告片地址
        map.put("filmPrevue",film.getFilmPrevue());
        // 所有场次
        List<Screening> screenings = screeningMapper.getScreeningByFilmId(filmId);
        map.put("screenings",screenings);
        // 用户评分
        List<Comments> comments = commentsMapper.findRootCommentsByFilmId(filmId);
        Float total = 0.0f;
        for (Comments comment : comments) {
            total += comment.getCommentsScore();
        }
        Float score = total/comments.size();
        map.put("score",score);
        // 假设电影没评论
        if(comments.size() == 0){
            map.put("score","暂无分数");
        }
        return map;
    }

    @Override
    public Screening getScreeningDetail(Integer screeningId) {
        return screeningMapper.getScreeningById(screeningId);
    }
}
